package com.springapp.utils;

import java.io.File;
import java.sql.ResultSet;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

import java.util.regex.Matcher;
import java.util.regex.Pattern;





/**
 * Created by Administrator on 14-11-11.
 * 通用工具类
 */
public class CommonTools {

    public static String old_database = "monitor";
    public static String new_database = "ActiveFM";


    //更新时间秒数 , 用于获取平均值
    public static int globaSeconds = 300;

    //设置配置文件路径  需要用到的参数 变量等 都将从该路径下的文件中获取
   /* public static String deployFilePath = ""; //在StartupListener中赋值 //C:\Developer\files\
    public static String SymmFile = ""; //在StartupListener中赋值
    public static String ClarFile = ""; //在StartupListener中赋值
    public static String BrocadeFile = ""; //在StartupListener中赋值
    public static String CiscoFile  = ""; //在StartupListener中赋值*/
    //;//= "D:\\agent\\";



    /**
     * 函数说明:格式化日期时间为“年-月-日 时：分：秒”的格式
     *
     * @param
     * @return String
     */
    public static String getNowDate() {
        Date date = new Date();
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String str = format.format(date);
        return str;
    }

    /**
     * 获取需要的时间格式  (yyyy-MM-dd HH:mm:ss) 可拆分
     * @param format
     * @return
     */
    public static String getNowDate(String format) {
        Date date = new Date();
        SimpleDateFormat fimpleDateFormat = new SimpleDateFormat(format);
        String str = fimpleDateFormat.format(date);
        return str;
    }




    /**
     * 根据容量数值算出占用总量的百分比
     * @param load_perce 使用量
     * @param free_perce 空闲量
     * @return
     */
    public static double getValue(Double load_perce,Double free_perce){
        Double allPerce = load_perce + free_perce;
        double  d= load_perce/allPerce*100;
        return Double.valueOf(d).doubleValue();
    }

    /**
     * 根据容量数值算出占用总量的百分比
     * @param load_perce 使用量
     * @param free_perce 空闲量
     * @return
     */
    public static int getValue(Long load_perce,Long free_perce){
        int i = 0 ;
        Long count = load_perce + free_perce ;
        float j = (float) load_perce / (float)count ;
        i = (int)(j*100);
        return i;
    }

    /**
     * 去掉字符串中的空格、回车、换行符、制表符
     *
     * @param str
     * @return
     */
    public static String replaceBlank(String str) {
        String dest = "";
        if (str != null) {
            Pattern p = Pattern.compile("\\s*|\t|\r|\n");
            Matcher m = p.matcher(str);
            dest = m.replaceAll("");
        }
        return dest;
    }

    /**
     * 多个空格替换为一个空格
     * @param str
     * @return
     */
    public static String replaceMultiBlank(String str){
        return str.replaceAll(" +"," ");
    }


	/*public static void main(String[] args) {
		System.out.println("---");
		//System.out.println(CommonTools.getValue( 18391040L,100147200L));
		//System.out.println(CommonTools.deployFilePath);
		CommonTools c = new CommonTools();
		c.sample();
	}

	public static void setDeployFilePath(){
		CommonTools c = new CommonTools();
		c.sample();
	}
	*/


	/*public void sample() {
		try {
			InputStream is=this.getClass().getResourceAsStream("deployFilePath.properties");
			//System.out.println();
			CommonTools.deployFilePath = HandleProperties.readValue(is, "deployFilePath");
		} catch (Exception e) {
			// TODO: handle exception
		}
    }*/


/*
    *//**
     * 根据xml文件路径,返回document对象
     * @param path
     *//*
    public static Document getDom4jDocument(String path) {
        Document document = null;
        SAXReader saxReader = new SAXReader();
        try {
            document = saxReader.read(new File(path));
        } catch (Exception e) {
            e.printStackTrace();
        }
        return document;
    }

    *//**
     * 通用判断, 查询数据库判断某个ID是否存在
     * @param sql : select * from a where b='1'
     * @return  true or false;
     *//*
    public static boolean judResultBoolean(String sql ){
        boolean bl = false;
        try {
            ResultSet rs = DataBase.select(sql);
            if ( rs.next() ) {
                bl = true;
            }
            DataBase.sourceClose(rs , null);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return bl;
    }*/

    /**
     * 判断当前时间是否是在当日最后五分钟内
     *
     * @return
     */
    public static boolean judgeEndDay() {
        return CommonTools.judgeEndDay(CommonTools.getNowDate("yyyy-MM-dd"));
    }

    /**
     * 根据指定年月日判断是否在当日最后五分钟内
     *
     * @param strDate
     *            指定年月日
     * @return
     */
    public static boolean judgeEndDay(String strDate) {
        boolean bl = false;
        try {

            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            // CommonTools.getNowDate("yyyy-MM-dd") 返回当前时间年月日的字符串
            Date startDate = sdf.parse(strDate + " 23:55:00");
            Date endDate = sdf.parse(strDate + " 23:59:59");
            Date nowDate = new Date();

            if ((nowDate.equals(startDate) || nowDate.after(startDate))
                    && (nowDate.equals(endDate) || nowDate.before(endDate))) {
                bl = true;
            }

			/*
			 * System.out.println("开始时间" + sdf.format(startDate));
			 * System.out.println("结束时间" + sdf.format(endDate));
			 * System.out.println("当前时间" + sdf.format(nowDate));
			 * System.out.println("是否在时间范围内:" + bl);
			 */
        } catch (Exception e) {
            e.printStackTrace();
        }
        return bl;
    }

    /**
     * 判断是否是当月最后五分钟
     *
     * @return
     */
    public static boolean judgeEndMon() {
        boolean bl = false;
        try {
            Calendar calendar = Calendar.getInstance();
            calendar.set(Calendar.DATE,
                    calendar.getActualMaximum(Calendar.DATE));
            // 打印
            DateFormat format = new SimpleDateFormat("yyyy-MM-dd");
            String nowDay = format.format(calendar.getTime());
            bl = CommonTools.judgeEndDay(nowDay);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return bl;
    }


    /**
     * String 转 long 并且处理
     * @param s
     * @return
     */
    public static long formatProType(String s){
        long l = 0l;
        try {
            l = Long.parseLong(s);
        } catch (Exception e) {
            l = 0l;
        }
        return l;
    }

    /**
     * TD 2016年2月18日
     * 替换字符串中关键的字符串
     * @param ele
     * @return
     */
    public static String primaryRep(String ele){
        ele = ele.replaceAll("&quot;", "");
        //ele = ele.replaceAll("", "");
        //ele = ele.replaceAll("", "");
        //ele = ele.replaceAll("", "");
        return ele;
    }



}
